﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using Security.ERP;
using Extra.ERP;
using Common.Base;
using Common.Class;
using Common;

namespace Security.Security
{
    public class xucGroupUser : xucBase
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiGroupAdd;
        private BarButtonItem bbiGroupDelete;
        private BarButtonItem bbiGroupEdit;
        private BarButtonItem bbiReload;
        private BarButtonItem bbiUserAdd;
        private BarButtonItem bbiUserDelete;
        private BarButtonItem bbiUserEdit;
        private GroupUser cls;
        private IContainer components = null;
        private DataTable dt;
        public DevExpress.Utils.ImageCollection imageCollection2;
        private DevExpress.Utils.ImageCollection imgTree;
        private Point m_Point;
        private PopupMenu ppGroupUser;
        private PopupMenu ppUser;
        private TreeList treeList1;
        private TreeListColumn treeListColumn2;

        public event SelectedEventHander Selected;

        public event UpdatedEventHander Updated;

        public xucGroupUser()
        {
            this.InitializeComponent();
        }

        private void bbiGroupAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.GroupAdd();
        }

        private void bbiGroupDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.GroupDelete();
        }

        private void bbiGroupEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.GroupEdit();
        }

        private void bbiReload_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Reload();
        }

        private void bbiUserAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.UserAdd();
        }

        private void bbiUserDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.UserDelete();
        }

        private void bbiUserEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.UserEdit();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void frm_Added(object sender, SYS_GROUP Item)
        {
            this.Reload();
        }

        private void frm_Added(object sender, SYS_USER Item)
        {
            this.Reload();
        }

        private void frm_Updated(object sender, SYS_GROUP Item)
        {
            this.Reload();
        }

        private void frm_Updated(object sender, SYS_USER Item)
        {
            this.Reload();
        }

        public void GroupAdd()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    MyRule.Notify();
                }
                else
                {
                    xfmGroupAdd add = new xfmGroupAdd(Actions.Add);
                    add.Added += new xfmGroupAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public void GroupDelete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    MyRule.Notify();
                }
                else if (!ClsOption.System2.IsQuestion || (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
                {
                    SYS_GROUP sys_group = new SYS_GROUP();
                    string str = "";
                    if (true)
                    {
                        object groupID = null;
                        groupID = this.cls.GroupID;
                        if (groupID != null)
                        {
                            SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Xo\x00e1", groupID.ToString());
                            if (groupID.ToString().ToLower() == "admin")
                            {
                                XtraMessageBox.Show("Nh\x00f3m người d\x00f9ng kh\x00f4ng n\x00e0y kh\x00f4ng được xo\x00e1.", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                base.CreateWaitDialog();
                                base.SetWaitDialogCaption("Đang x\x00f3a...");
                                str = sys_group.Delete(groupID.ToString());
                                if (str == "OK")
                                {
                                    this.Reload();
                                }
                                else if (str != "OK")
                                {
                                    MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                }
                                this.DoHide();
                            }
                        }
                    }
                }
            }
        }

        public void GroupEdit()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    MyRule.Notify();
                }
                else
                {
                    SYS_GROUP item = new SYS_GROUP();
                    object groupID = this.cls.GroupID;
                    if (groupID != null)
                    {
                        base.CreateWaitDialog();
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        if (item.Get(groupID.ToString()) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            xfmGroupAdd add = new xfmGroupAdd(Actions.Update, item);
                            add.Updated += new xfmGroupAdd.UpdatedEventHander(this.frm_Updated);
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucGroupUser));
            this.treeList1 = new TreeList();
            this.treeListColumn2 = new TreeListColumn();
            this.imgTree = new DevExpress.Utils.ImageCollection(this.components);
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.imageCollection2 = new DevExpress.Utils.ImageCollection(this.components);
            this.bbiGroupAdd = new BarButtonItem();
            this.bbiUserAdd = new BarButtonItem();
            this.bbiUserEdit = new BarButtonItem();
            this.bbiGroupEdit = new BarButtonItem();
            this.bbiUserDelete = new BarButtonItem();
            this.bbiGroupDelete = new BarButtonItem();
            this.bbiReload = new BarButtonItem();
            this.ppGroupUser = new PopupMenu(this.components);
            this.ppUser = new PopupMenu(this.components);
            this.treeList1.BeginInit();
            this.imgTree.BeginInit();
            this.barManager1.BeginInit();
            this.imageCollection2.BeginInit();
            this.ppGroupUser.BeginInit();
            this.ppUser.BeginInit();
            base.SuspendLayout();
            this.treeList1.Appearance.FocusedCell.BackColor = Color.Orange;
            this.treeList1.Appearance.FocusedCell.BackColor2 = Color.White;
            this.treeList1.Appearance.FocusedCell.GradientMode = LinearGradientMode.BackwardDiagonal;
            this.treeList1.Appearance.FocusedCell.Options.UseBackColor = true;
            this.treeList1.Columns.AddRange(new TreeListColumn[] { this.treeListColumn2 });
            this.treeList1.Dock = DockStyle.Fill;
            this.treeList1.Location = new Point(0, 0);
            this.treeList1.Name = "treeList1";
            this.treeList1.OptionsBehavior.PopulateServiceColumns = true;
            this.treeList1.OptionsView.ShowHorzLines = false;
            this.treeList1.OptionsView.ShowVertLines = false;
            this.treeList1.SelectImageList = this.imgTree;
            this.treeList1.Size = new Size(0x11a, 0x1f2);
            this.treeList1.TabIndex = 2;
            this.treeList1.MouseDown += new MouseEventHandler(this.treeList1_MouseDown);
            this.treeList1.FocusedNodeChanged += new FocusedNodeChangedEventHandler(this.treeList1_FocusedNodeChanged);
            this.treeList1.GetSelectImage += new GetSelectImageEventHandler(this.treeList1_GetSelectImage);
            this.treeListColumn2.Caption = "Ph\x00e2n quyền người d\x00f9ng";
            this.treeListColumn2.FieldName = "Name";
            this.treeListColumn2.Name = "treeListColumn2";
            this.treeListColumn2.OptionsColumn.AllowEdit = false;
            this.treeListColumn2.OptionsColumn.ReadOnly = true;
            this.treeListColumn2.Visible = true;
            this.treeListColumn2.VisibleIndex = 0;
            this.imgTree.ImageStream = (ImageCollectionStreamer)manager.GetObject("imgTree.ImageStream");
            this.imgTree.Images.SetKeyName(0, "1280287512_User group.png");
            this.imgTree.Images.SetKeyName(1, "male.png");
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Images = this.imageCollection2;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiGroupAdd, this.bbiUserAdd, this.bbiUserEdit, this.bbiGroupEdit, this.bbiUserDelete, this.bbiGroupDelete, this.bbiReload });
            this.barManager1.MaxItemId = 30;
            this.imageCollection2.ImageStream = (ImageCollectionStreamer)manager.GetObject("imageCollection2.ImageStream");
            this.imageCollection2.Images.SetKeyName(7, "Them.png");
            this.imageCollection2.Images.SetKeyName(10, "plus.png");
            this.bbiGroupAdd.Caption = "Th\x00eam nh\x00f3m người d\x00f9ng";
            this.bbiGroupAdd.Id = 0x17;
            this.bbiGroupAdd.ImageIndex = 10;
            this.bbiGroupAdd.Name = "bbiGroupAdd";
            this.bbiGroupAdd.ItemClick += new ItemClickEventHandler(this.bbiGroupAdd_ItemClick);
            this.bbiUserAdd.Caption = "Th\x00eam người d\x00f9ng";
            this.bbiUserAdd.Id = 0x18;
            this.bbiUserAdd.ImageIndex = 7;
            this.bbiUserAdd.Name = "bbiUserAdd";
            this.bbiUserAdd.ItemClick += new ItemClickEventHandler(this.bbiUserAdd_ItemClick);
            this.bbiUserEdit.Caption = "Sửa người d\x00f9ng";
            this.bbiUserEdit.Id = 0x19;
            this.bbiUserEdit.ImageIndex = 4;
            this.bbiUserEdit.Name = "bbiUserEdit";
            this.bbiUserEdit.ItemClick += new ItemClickEventHandler(this.bbiUserEdit_ItemClick);
            this.bbiGroupEdit.Caption = "Sửa nh\x00f3m người d\x00f9ng";
            this.bbiGroupEdit.Id = 0x1a;
            this.bbiGroupEdit.ImageIndex = 4;
            this.bbiGroupEdit.Name = "bbiGroupEdit";
            this.bbiGroupEdit.ItemClick += new ItemClickEventHandler(this.bbiGroupEdit_ItemClick);
            this.bbiUserDelete.Caption = "X\x00f3a người d\x00f9ng";
            this.bbiUserDelete.Id = 0x1b;
            this.bbiUserDelete.ImageIndex = 3;
            this.bbiUserDelete.Name = "bbiUserDelete";
            this.bbiUserDelete.ItemClick += new ItemClickEventHandler(this.bbiUserDelete_ItemClick);
            this.bbiGroupDelete.Caption = "X\x00f3a nh\x00f3m người d\x00f9ng";
            this.bbiGroupDelete.Id = 0x1c;
            this.bbiGroupDelete.ImageIndex = 3;
            this.bbiGroupDelete.Name = "bbiGroupDelete";
            this.bbiGroupDelete.ItemClick += new ItemClickEventHandler(this.bbiGroupDelete_ItemClick);
            this.bbiReload.Caption = "Nạp lại";
            this.bbiReload.Id = 0x1d;
            this.bbiReload.ImageIndex = 6;
            this.bbiReload.Name = "bbiReload";
            this.bbiReload.ItemClick += new ItemClickEventHandler(this.bbiReload_ItemClick);
            this.ppGroupUser.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiUserAdd), new LinkPersistInfo(this.bbiGroupAdd, true), new LinkPersistInfo(this.bbiGroupEdit), new LinkPersistInfo(this.bbiGroupDelete), new LinkPersistInfo(this.bbiReload, true) });
            this.ppGroupUser.Manager = this.barManager1;
            this.ppGroupUser.Name = "ppGroupUser";
            this.ppUser.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiUserAdd, true), new LinkPersistInfo(this.bbiGroupAdd, true), new LinkPersistInfo(this.bbiUserEdit), new LinkPersistInfo(this.bbiUserDelete), new LinkPersistInfo(this.bbiReload, true) });
            this.ppUser.Manager = this.barManager1;
            this.ppUser.Name = "ppUser";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.treeList1);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucGroupUser";
            base.Size = new Size(0x11a, 0x1f2);
            this.treeList1.EndInit();
            this.imgTree.EndInit();
            this.barManager1.EndInit();
            this.imageCollection2.EndInit();
            this.ppGroupUser.EndInit();
            this.ppUser.EndInit();
            base.ResumeLayout(false);
        }

        public void LoadData()
        {
            this.LoadTreeList();
        }

        private void LoadTreeList()
        {
            DataTable list = new SYS_GROUP().GetList();
            this.dt = new DataTable();
            this.dt.Columns.Add("ID");
            this.dt.Columns.Add("Name");
            this.dt.Columns.Add("ParentID");
            this.dt.Columns.Add("ImageIndex");
            foreach (DataRow row in list.Rows)
            {
                if (row["Group_ID"].ToString() != "employee")
                {
                    this.dt.Rows.Add(new object[] { row["Group_ID"].ToString(), row["Group_Name"].ToString(), "", 0 });
                    DataTable table2 = new SYS_USER().GetList(row["Group_ID"].ToString());
                    foreach (DataRow row2 in table2.Rows)
                    {
                        this.dt.Rows.Add(new object[] { row2["UserID"].ToString(), row2["UserName"].ToString(), row["Group_ID"].ToString(), 1 });
                    }
                }
            }
            this.treeList1.DataSource = this.dt;
            this.treeList1.ExpandAll();
        }

        public void RaiseSelectedEventHander(GroupUser Item)
        {
            if (this.Selected != null)
            {
                this.Selected(this, Item);
            }
        }

        public void RaiseUpdatedEventHander()
        {
            if (this.Updated != null)
            {
                this.Updated(this);
            }
        }

        public void Reload()
        {
            this.LoadData();
        }

        private void ShowMenu(TreeListHitInfo hi, Point point)
        {
            if (hi.Node != null)
            {
                if (hi.Node.Level == 0)
                {
                    this.ppGroupUser.ShowPopup(point);
                }
                else if (hi.Node.Level == 1)
                {
                    this.ppUser.ShowPopup(point);
                }
            }
            this.treeList1.SetFocusedNode(hi.Node);
        }

        private void treeList1_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            DataRowView dataRecordByNode = (DataRowView)this.treeList1.GetDataRecordByNode(e.Node);
            this.cls = new GroupUser();
            if (e.Node != null)
            {
                if (e.Node.Level == 0)
                {
                    this.cls.GroupID = dataRecordByNode["ID"].ToString();
                    this.cls.Level = 0;
                    this.RaiseSelectedEventHander(this.cls);
                }
                else if (e.Node.Level == 1)
                {
                    this.cls.UserID = dataRecordByNode["ID"].ToString();
                    this.cls.Level = 1;
                    this.RaiseSelectedEventHander(this.cls);
                }
            }
            this.treeList1.SetFocusedNode(e.Node);
        }

        private void treeList1_GetSelectImage(object sender, GetSelectImageEventArgs e)
        {
            DataRowView dataRecordByNode = (DataRowView)this.treeList1.GetDataRecordByNode(e.Node);
            e.NodeImageIndex = int.Parse(dataRecordByNode["ImageIndex"].ToString());
        }

        private void treeList1_MouseDown(object sender, MouseEventArgs e)
        {
            this.m_Point = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Right)
            {
                this.ShowMenu(this.treeList1.CalcHitInfo(this.m_Point), this.treeList1.PointToScreen(this.m_Point));
            }
        }

        public void UserAdd()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    xfmUserAdd add = new xfmUserAdd(Actions.Add);
                    add.Added += new xfmUserAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public void UserDelete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (XtraMessageBox.Show(MsgResc.Delete, "Cảnh B\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
                {
                    SYS_USER sys_user = new SYS_USER();
                    string str = "";
                    if (true)
                    {
                        object userID = null;
                        userID = this.cls.UserID;
                        if (userID != null)
                        {
                            SYS_LOG.Insert("Quản L\x00fd Người D\x00f9ng", "Xo\x00e1", userID.ToString());
                            if (userID.ToString() == MyLogin.UserId)
                            {
                                XtraMessageBox.Show("Kh\x00f4ng được x\x00f3a người d\x00f9ng đang sử dụng.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else if (userID.ToString() == "admin")
                            {
                                XtraMessageBox.Show("TKh\x00f4ng được x\x00f3a người d\x00f9ng hệ thống.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                base.CreateWaitDialog();
                                base.SetWaitDialogCaption("Đang x\x00f3a...");
                                str = sys_user.Delete(userID.ToString());
                                if (str == "OK")
                                {
                                    this.Reload();
                                }
                                else if (str != "OK")
                                {
                                    XtraMessageBox.Show("Lỗi:\n\t" + str, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                this.DoHide();
                            }
                        }
                    }
                }
            }
        }

        public void UserEdit()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    SYS_USER item = new SYS_USER();
                    object userID = this.cls.UserID;
                    if (userID != null)
                    {
                        base.CreateWaitDialog();
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        if (item.Get(userID.ToString()) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            xfmUserAdd add = new xfmUserAdd(Actions.Update, item, false);
                            add.Updated += new xfmUserAdd.UpdatedEventHander(this.frm_Updated);
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        public delegate void SelectedEventHander(object sender, GroupUser Item);

        public delegate void UpdatedEventHander(object sender);
    }
}
